package org.youchan.aco;

import java.util.List;
import java.util.logging.Logger;

import org.youchan.aco.model.Member;
import org.youchan.aco.model.MemberLevel;
import org.youchan.aco.model.MemberProfile;
import org.youchan.aco.model.MembersList;
import org.youchan.aco.p2p.A2AManager;
import org.youchan.aco.p2p.A2AManager.A2AManagerListener;
import org.youchan.aco.twitter.TwitterManager;
import org.youchan.util.MessageHandler;
import org.youchan.util.MessagingQueue;

import twitter4j.TwitterException;
import twitter4j.User;

import com.skype.Friend;
import com.skype.SkypeException;

public class MembersListManager implements A2AManagerListener, MessageHandler<MembersListCommand> {
	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(MembersListManager.class.getCanonicalName());
	private static MembersListManager self_;
	
	private static final String MEMBERS_DB = "db/members.dat";
	
	public static final MembersListCommand FETCH_SKYPE_FRIENDS_COMMAND = 
		new MembersListCommandAdapter(MembersListCommand.Type.FETCH_SKYPE_FRIENDS);
	public static final MembersListCommand FETCH_TWITTER_FRIENDS_COMMAND = 
		new MembersListCommandAdapter(MembersListCommand.Type.FETCH_TWITTER_FRIENDS);

	private MembersList mMemberList;
	private MessagingQueue<MembersListCommand> mQueue;

	private MembersListManager() {
		mMemberList = new MembersList(MEMBERS_DB);
		mQueue = new MessagingQueue<MembersListCommand>(this);
		mQueue.start();
		init();
	}
	
	private void init() {
		A2AManager.getInstance().addA2AManagerListener(this);
		mQueue.sendMessage(FETCH_SKYPE_FRIENDS_COMMAND);
		mQueue.sendMessage(FETCH_TWITTER_FRIENDS_COMMAND);
	}
	
	public static MembersListManager getInstance() {
		if (self_ == null) {
			self_ = new MembersListManager();
		}
		return self_;
	}
	
	public MembersList getMembersList() {
		return mMemberList;
	}
	
	public void sendCommand(MembersListCommand command) {
		mQueue.sendMessage(command);
	}
	
	private Member addTwitterFriend(User user) {
		Member member = mMemberList.getMemberByTwitterId(user.getScreenName());
		if (member == null) {
			member = new Member();
			member.setTwitterId(user.getScreenName());
			member.setMemberLevel(MemberLevel.TWITTER);
			mMemberList.addMember(member);
		}
		return member;
	}
	
	private void fetchTwitterFriends() throws TwitterException {
		TwitterManager tm = TwitterManager.getInstance();
		List<User> users = tm.getFriends();
		for (User user: users) {
			addTwitterFriend(user);
		}
	}
	
	private Member addSkypeFriend(Friend friend) {
		Member member = mMemberList.getMemberByMemberId(friend.getId());
		if (member == null) {
			member = new Member();
			member.setMemberId(friend.getId());
			member.setMemberLevel(MemberLevel.CONTACT);
			mMemberList.addMember(member);
		}		
		return member;
	}
	
	private void fetchSkypeFriends() {
		A2AManager a2aManager = A2AManager.getInstance();
		try {
			Friend[] friends = a2aManager.getConnectableFriend();
			for (Friend friend: friends) {
				addSkypeFriend(friend);
			}
		} catch (SkypeException e) {
			e.printStackTrace();
		}
	}
	
	private Member fetchMemberProfile(Member member) throws TwitterException {
		MemberProfile profile = TwitterManager.getInstance().getMemberProfile(member.getTwitterId());
		member.setProfile(profile);
		mMemberList.addMember(member);
		return member;
	}

	public void handle(MembersListCommand command) {
		try {
			switch (command.type) {
			case FETCH_SKYPE_FRIENDS:
				fetchSkypeFriends();
				break;
			case ADD_MEMBER:
				mMemberList.addMember(command.getMember());
				break;
			case REMOVE_MEMBER:
				mMemberList.removeMemberByMemberId(command.getMember().getMemberId());
				break;
			case FETCH_TWITTER_FRIENDS:
				fetchTwitterFriends();
				break;
			case FETCH_TWITTER_FRIENDS_PROFILE:
				fetchMemberProfile(command.getMember());
				break;
			}
			command.done(command, mMemberList);
		} catch (Exception e) {
			command.occuredException(command, mMemberList, e);
		}
	}
	
//	public Image getImageByTwitterId(String twitterId) {
//		Member member = getMembersList().getMemberByTwitterId(twitterId);
//		if (member == null) {
//			member = new Member();
//			member.setTwitterId(twitterId);
//			member.setMemberLevel(MemberLevel.TWITTER);
//		}
//		MemberProfile profile = member.getProfile();
//		if (profile == null || profile.getImageURL() == null) {
//			try {
//				profile = TwitterManager.getInstance().getMemberProfile(twitterId);
//			} catch (TwitterException e) {
//				e.printStackTrace();
//				return null;
//			}
//		}
//		
//		try {
//			URL url = profile.getImageURL();
//			if (url != null) {
//				Image image = mImageCache.getImage(url);
//				return image;
//			}
//		} catch (MalformedURLException e) {
//			member.setProfile(null);
//			mMemberList.addMember(member);
//			e.printStackTrace();
//		} catch (IOException e) {
//			member.setProfile(null);
//			mMemberList.addMember(member);
//			e.printStackTrace();
//		} catch (NullPointerException e) {
//			member.setProfile(null);
//			mMemberList.addMember(member);
//			e.printStackTrace();
//		}
//		return null;
//	}

	public void memberConnected(String memberId) {
		Member member = new Member();
		member.setMemberId(memberId);
		member.setMemberLevel(MemberLevel.CONTACT);
		sendCommand(new MembersListCommandAdapter(MembersListCommand.Type.ADD_MEMBER, member));
	}

	public void memberDisconnected(String memberId) {
		Member member = new Member();
		member.setMemberId(memberId);
		sendCommand(new MembersListCommandAdapter(MembersListCommand.Type.REMOVE_MEMBER, member));
	}
}
